﻿using DevExpress.XtraEditors;
using MDK.Device;
using MDK.Device.Devices;
using MDK.Winform;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MDK.Winform.UserControl.Config
{
    public partial class Pre : BaseForm
    {
        #region 初始化
        public Pre()
        {
            InitializeComponent();
        }

        private void Pre_Load(object sender, EventArgs e)
        {
            InitSpeed();
        }

        public void InitSpeed()
        {
            txtXspeed.Text = App.flow.SysModel.Front.X.Speed.ToString();
            txtYspeed.Text = App.flow.SysModel.Front.Y.Speed.ToString();
            txtZspeed.Text = App.flow.SysModel.Front.Z.Speed.ToString();
        }

        #endregion

        #region 刷新设备状态
        public void RefreshDeviceStatus()
        {
            while (true)
            {
                try
                {
                    Thread.Sleep(1000);
                    if (this.IsHandleCreated)
                    {
                        //工位
                        for (int i = 0; i < App.flow.SysModel.Front.PositionSensors.Count(); i++)
                        {
                            var control = ((Signal)(this.Controls.Find("Sensor" + i, true)[0]));
                            control.Invoke(new MethodInvoker(() =>
                            {
                                if (App.flow.SysModel.Front.PositionSensors[i].IsActive())
                                    control.Set2();
                                else
                                    control.Set1();
                            }));
                        }

                        //模型
                        Sensor41.Invoke(new MethodInvoker(() =>
                        {
                            if (App.flow.SysModel.Front.Device.IsAutoMode)
                                Sensor41.Set2();
                            else if (App.flow.SysModel.Front.Device.IsManualMode)
                                Sensor41.Set1();
                            else
                                Sensor41.Set3();
                        }));
                        //急停
                        Sensor42.Invoke(new MethodInvoker(() =>
                        {
                            if (App.flow.SysModel.Front.Device.IsScrammed)
                                Sensor42.Set2();
                            else
                                Sensor42.Set1();
                        }));

                        //光幕
                        Sensor43.Invoke(new MethodInvoker(() =>
                        {
                            if (App.flow.SysModel.Front.Raster.IsActive())
                                Sensor43.Set2();
                            else
                                Sensor43.Set1();
                        }));

                        //紫外灯
                        Sensor44.Invoke(new MethodInvoker(() =>
                        {
                            if (App.flow.SysModel.Front.UVLamp.IsOn())
                                Sensor44.Set2();
                            else
                                Sensor44.Set1();
                        }));

                        //照明灯
                        Sensor45.Invoke(new MethodInvoker(() =>
                        {
                            if (App.flow.SysModel.Front.Lamp.IsOn())
                                Sensor45.Set2();
                            else
                                Sensor45.Set1();
                        }));

                        //手指
                        Sensor48.Invoke(new MethodInvoker(() =>
                        {
                            if (App.flow.SysModel.Front.Lamp.IsOn())
                                Sensor48.Set2();
                            else
                                Sensor48.Set1();
                        }));

                        //传感器
                        Sensor49.Invoke(new MethodInvoker(() =>
                        {
                            if (App.flow.SysModel.Front.X.MaximumSensor.IsActive())
                                Sensor49.Set2();
                            else
                                Sensor49.Set1();
                        }));

                        //传感器
                        Sensor50.Invoke(new MethodInvoker(() =>
                        {
                            if (App.flow.SysModel.Front.X.MinimumSensor.IsActive())
                                Sensor50.Set2();
                            else
                                Sensor50.Set1();
                        }));
                        //传感器
                        Sensor51.Invoke(new MethodInvoker(() =>
                        {
                            if (App.flow.SysModel.Front.X.MaximumSensor.IsActive())
                                Sensor51.Set2();
                            else
                                Sensor51.Set1();
                        }));

                        //传感器
                        Sensor52.Invoke(new MethodInvoker(() =>
                        {
                            if (App.flow.SysModel.Front.Y.MinimumSensor.IsActive())
                                Sensor52.Set2();
                            else
                                Sensor52.Set1();
                        }));
                        //传感器
                        Sensor53.Invoke(new MethodInvoker(() =>
                        {
                            if (App.flow.SysModel.Front.Z.MaximumSensor.IsActive())
                                Sensor53.Set2();
                            else
                                Sensor53.Set1();
                        }));

                        //传感器
                        Sensor54.Invoke(new MethodInvoker(() =>
                        {
                            if (App.flow.SysModel.Front.Z.MinimumSensor.IsActive())
                                Sensor54.Set2();
                            else
                                Sensor54.Set1();
                        }));
                        txtXCurcoord.Invoke(new MethodInvoker(() =>
                        {
                            txtXCurcoord.Text = App.flow.SysModel.Front.X.CurrentCoord.ToString();
                        }));

                        txtYCurcoord.Invoke(new MethodInvoker(() =>
                        {
                            txtYCurcoord.Text = App.flow.SysModel.Front.Y.CurrentCoord.ToString();
                        }));
                        txtZCurcoord.Invoke(new MethodInvoker(() =>
                        {
                            txtZCurcoord.Text = App.flow.SysModel.Front.Z.CurrentCoord.ToString();
                        }));

                    }
                }
                catch (Exception ex)
                {

                }
            }
        }
        #endregion

        #region --详细操作--
        #region 模式切换
        private void btnAuto_Click(object sender, EventArgs e)
        {
            App.flow.SysModel.Front.Device.SwitchAutoMode();
        }

        private void btnMu_Click(object sender, EventArgs e)
        {
            App.flow.SysModel.Front.Device.SwitchManualMode();
        }
        #endregion

        #region 急停
        private void btnOpenScrammed_Click(object sender, EventArgs e)
        {

        }

        private void btnCloseScrammed_Click(object sender, EventArgs e)
        {

        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            App.flow.SysModel.Front.Device.ClearError();
        }
        #endregion

        #region 紫外灯
        private void btnOpenU_Click(object sender, EventArgs e)
        {
            App.flow.SysModel.Front.UVLamp.TurnOn();
        }

        private void btnCloseU_Click(object sender, EventArgs e)
        {
            App.flow.SysModel.Front.UVLamp.TurnOff();
        }

        #endregion

        #region 照明
        private void btnOpenLight_Click(object sender, EventArgs e)
        {
            App.flow.SysModel.Front.Lamp.TurnOn();
        }

        private void btnCloseLight_Click(object sender, EventArgs e)
        {
            App.flow.SysModel.Front.Lamp.TurnOff();
        }

        #endregion

        #region 称重 

        private void btnWeight_Click(object sender, EventArgs e)
        {
            txtWeight.Text = App.flow.SysModel.Front.Weigher.GetWeight().ToString();
        }
        #endregion

        #region 手指
        private void btnOnFinger_Click(object sender, EventArgs e)
        {
            App.flow.SysModel.Front.Finger.TurnOff();
        }

        private void btnOffFinger_Click(object sender, EventArgs e)
        {
            App.flow.SysModel.Front.Finger.TurnOn();
        }

        #endregion

        #region X
        private void btnXsetSpeed_Click(object sender, EventArgs e)
        {
            int i = 0;
            int.TryParse(txtXspeed.Text.Trim(), out i);
            if (i == 0)
            {
                FastMessageBox.Show("请检查输入格式");
            }
            else
            {
                App.flow.SysModel.Front.X.Speed = i;
            }
        }

        private void btnXGo_MouseDown(object sender, MouseEventArgs e)
        {
            App.flow.SysModel.Front.X.StartIncrease();
        }

        private void btnXGo_MouseUp(object sender, MouseEventArgs e)
        {
            App.flow.SysModel.Front.X.StopIncrease();
        }

        private void btnXBack_MouseDown(object sender, MouseEventArgs e)
        {
            App.flow.SysModel.Front.X.StartDecrease();
        }

        private void btnXBack_MouseUp(object sender, MouseEventArgs e)
        {
            App.flow.SysModel.Front.X.StopDecrease();
        }

        private void btnXWrite_Click(object sender, EventArgs e)
        {
            int i = 0;
            int.TryParse(txtXCoord.Text.Trim(), out i);
            if (i == 0)
            {
                FastMessageBox.Show("请检查输入格式");
            }
            else
            {
                if (App.flow.SysModel.Front.X.MinimumLimit > i && App.flow.SysModel.Front.X.MaximumLimit < i)
                {
                    App.flow.SysModel.Front.X.Goto(i);
                    App.flow.SysModel.Front.X.WaitCompleted();
                }
            }
        }

        private void btnXTurntoZero_Click(object sender, EventArgs e)
        {
            App.flow.SysModel.Front.X.StartReturnZero();
            App.flow.SysModel.Front.X.WaitCompleted();
        }

        #endregion

        #region Y

        private void btnYsetSpeed_Click(object sender, EventArgs e)
        {
            int i = 0;
            int.TryParse(txtYspeed.Text.Trim(), out i);
            if (i == 0)
            {
                FastMessageBox.Show("请检查输入格式");
            }
            else
            {
                App.flow.SysModel.Front.Y.Speed = i;
            }
        }

        private void btnYGo_MouseDown(object sender, MouseEventArgs e)
        {
            App.flow.SysModel.Front.Y.StartIncrease();
        }

        private void btnYGo_MouseUp(object sender, MouseEventArgs e)
        {
            App.flow.SysModel.Front.Y.StopIncrease();
        }

        private void btnYBack_MouseDown(object sender, MouseEventArgs e)
        {
            App.flow.SysModel.Front.Y.StartDecrease();
        }

        private void btnYBack_MouseUp(object sender, MouseEventArgs e)
        {
            App.flow.SysModel.Front.Y.StopDecrease();
        }
        private void btnYWrite_Click(object sender, EventArgs e)
        {
            int i = 0;
            int.TryParse(txtYCoord.Text.Trim(), out i);
            if (i == 0)
            {
                FastMessageBox.Show("请检查输入格式");
            }
            else
            {
                if (App.flow.SysModel.Front.Y.MinimumLimit > i && App.flow.SysModel.Front.Y.MaximumLimit < i)
                {
                    App.flow.SysModel.Front.Y.Goto(i);
                    App.flow.SysModel.Front.Y.WaitCompleted();
                }
            }
        }

        private void btnYTurntoZero_Click(object sender, EventArgs e)
        {
            App.flow.SysModel.Front.Y.StartReturnZero();
            App.flow.SysModel.Front.Y.WaitCompleted();
        }
        #endregion

        #region Z

        private void btnZsetSpeed_Click(object sender, EventArgs e)
        {
            int i = 0;
            int.TryParse(txtZspeed.Text.Trim(), out i);
            if (i == 0)
            {
                FastMessageBox.Show("请检查输入格式");
            }
            else
            {
                App.flow.SysModel.Front.Z.Speed = i;
            }
        }

        private void btnZGo_MouseDown(object sender, MouseEventArgs e)
        {
            App.flow.SysModel.Front.Z.StartIncrease();
        }

        private void btnZGo_MouseUp(object sender, MouseEventArgs e)
        {
            App.flow.SysModel.Front.Z.StopIncrease();
        }

        private void btnZBack_MouseDown(object sender, MouseEventArgs e)
        {
            App.flow.SysModel.Front.Z.StartDecrease();
        }

        private void btnZBack_MouseUp(object sender, MouseEventArgs e)
        {
            App.flow.SysModel.Front.Z.StopDecrease();
        }

        private void btnZWrite_Click(object sender, EventArgs e)
        {
            int i = 0;
            int.TryParse(txtZCoord.Text.Trim(), out i);
            if (i == 0)
            {
                FastMessageBox.Show("请检查输入格式");
            }
            else
            {
                if (App.flow.SysModel.Front.Z.MinimumLimit > i && App.flow.SysModel.Front.Z.MaximumLimit < i)
                {
                    App.flow.SysModel.Front.Z.Goto(i);
                    App.flow.SysModel.Front.Z.WaitCompleted();
                }
            }
        }

        private void btnZTurntoZero_Click(object sender, EventArgs e)
        {
            App.flow.SysModel.Front.Z.StartReturnZero();
            App.flow.SysModel.Front.Z.WaitCompleted();
        }
        #endregion

        #region SZ
        private void btnSZWrite_Click(object sender, EventArgs e)
        {
            int i = 0;
            int.TryParse(txtSZCoord.Text.Trim(), out i);
            if (i == 0)
            {
                FastMessageBox.Show("请检查输入格式");
            }
            else
            {
                App.flow.SysModel.Front.SZ.Goto(i);
                App.flow.SysModel.Front.SZ.WaitCompleted();
            }
        }

        private void btnSZTurntoZero_Click(object sender, EventArgs e)
        {
            App.flow.SysModel.Front.SZ.StartReturnZero();
            App.flow.SysModel.Front.SZ.WaitCompleted();
        }
        #endregion

        #region 触发器
        private void btnTrigger1_Click(object sender, EventArgs e)
        {
            App.flow.SysModel.Front.FingerScanner.Trigger();
           MessageBox.Show(App.flow.SysModel.Front.FingerScanner.WaitCompleted());
            App.flow.SysModel.Front.FingerScanner.Cancel();
        }

        private void btnCancel1_Click(object sender, EventArgs e)
        {
            App.flow.SysModel.Front.FingerScanner.Cancel();
        }

        private void btnTrigger2_Click(object sender, EventArgs e)
        {
            App.flow.SysModel.Front.FreezerBeltScanner.Trigger();
            App.flow.SysModel.Front.FreezerBeltScanner.WaitCompleted();
        }

        private void btnCancel2_Click(object sender, EventArgs e)
        {
            App.flow.SysModel.Front.FingerScanner.Cancel();
        }





        #endregion

        #endregion
    }
}
